package com.android.widget.swipelistview;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;

import com.android.widget.demo.R;

import java.util.ArrayList;
import java.util.List;

public class SwipeListViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe_list_view);

//        final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
//                R.array.planets_array, android.R.layout.simple_list_item_1);
        SlideCutListView listView = (SlideCutListView)findViewById(R.id.swipe_listview);
        SwipeDismissListView listView1 = (SwipeDismissListView)findViewById(R.id.swipe_listview2);

        List<String> dataSourceList = new ArrayList<String>();
        for (int i = 0; i < 20; i++) {
            dataSourceList.add("Data List" + i);
        }

        final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1,
                android.R.id.text1, dataSourceList);

//        listView.setAdapter(adapter);
//        listView.setRemoveListener(new SlideCutListView.RemoveListener() {
//            @Override
//            public void removeItem(SlideCutListView.RemoveDirection direction, int position) {
//                adapter.remove(adapter.getItem(position));
//            }
//        });

        //SwipeDismissListView is good for work.
        listView1.setAdapter(adapter);
        listView1.setOnDismissCallback(new SwipeDismissListView.OnDismissCallback() {
            @Override
            public void onDismiss(int dismissPosition) {
                adapter.remove(adapter.getItem(dismissPosition));
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_swipe_list_view, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
